﻿using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace ZilLion.Service.Core.Domain.EntityBase.JsonConverter
{
    public class ChinaDateTimeConverter : DateTimeConverterBase
    {
        private static readonly IsoDateTimeConverter dtConverter =
            new IsoDateTimeConverter {DateTimeFormat = "yyyy-MM-dd HH:mm:ss"};

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
            JsonSerializer serializer)
        {
            try
            {
                return dtConverter.ReadJson(reader, objectType, existingValue, serializer);
            }
            catch (Exception)
            {
                return DateTime.MinValue;
            }
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            dtConverter.WriteJson(writer, value, serializer);
        }
    }
}